#include "../tools.h"



class Solution {
private:
    vector<vector<int>> direction = {{0,1}, {1,0}, {0,-1}, {-1,0}};
public:
    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
        int n = rows * cols;
        vector<vector<int>> res(n, vector<int>(2));
        res[0][0] = rStart;
        res[0][1] = cStart;
        int i = 1;
        int l = 1;
        int step = 0;
        int r = rStart, c = cStart;
        while(i < n){
            // 每趟移动 l 步
            for(int j = 0; j < l; ++j){
                r += direction[step][0];
                c += direction[step][1];
                if(r < rows && r >= 0 && c < cols && c >= 0){
                    res[i][0] = r;
                    res[i][1] = c;
                    ++i;
                    if(i == n) return res;
                }
            }
            l += (step % 2);
            step = (step + 1) % 4;
        }


        return res;
    }
};


int main(int argc, char const *argv[])
{
    Solution sol;

    int R = 1;
    int C = 4;
    int r0 = 0;
    int c0 = 0;

    auto res = sol.spiralMatrixIII(R, C, r0, c0);
    show(res);



    return 0;
}
